package com.cjzuji.rental.Config;

import com.cjzuji.rental.Bean.ApiResponse;
import com.cjzuji.rental.exception.BusinessException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author bag
 * @version V1.0
 * @date 2025/6/16 16:17
 * @since 1.0
 */
//全局异常处理器 GlobalExceptionHandler
@RestControllerAdvice
public class GlobalExceptionHandler {

    // 处理业务异常
    @ExceptionHandler(BusinessException.class)
    public ApiResponse<?> handleBusinessException(BusinessException ex) {
        return ApiResponse.fail(ex.getCode(), ex.getMessage());
    }

    // 处理其他未捕获异常
    @ExceptionHandler(Exception.class)
    public ApiResponse<?> handleException(Exception ex) {
        return ApiResponse.fail("500", "系统繁忙，请稍后重试");
        // 生产环境建议隐藏详细错误，日志记录ex.printStackTrace()
    }
}